<template>
  <div id="order">
    <div class="search">
      <el-form ref="searchForm" :model="searchForm" inline label-width="90px">
        <el-form-item label="文章栏目:" prop="cate_id">
          <el-cascader v-model="searchForm.cate_id" placeholder="请选择" :options="options"
            :props="{ value: 'id', label: 'name' }" :show-all-levels="false" @change="handleChange" />
        </el-form-item>
        <el-form-item :label="`${$t('文章标题')}：`">
          <el-input v-model="searchForm.title" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search(searchForm)">查询</el-button>
          <el-button @click="reset(searchForm)">{{ $t('重置') }}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <h3 class="headtitle">
      <span />
      素材管理
    </h3>
    <el-button type="primary" style="margin-bottom: 15px" @click="goAdd">
      新增文章
    </el-button>
    <el-table :data="orderData" border stripe :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }">
      <el-table-column label="ID" prop="id" width="60px" />
      <el-table-column label="文章标题" prop="title" show-overflow-tooltip width="250px" />
      <el-table-column label="分类名称" prop="name" />
      <el-table-column label="简介" prop="intro" :show-overflow-tooltip="true" />
      <el-table-column :label="$t('状态')" width="250px">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" :active-text="$t('正常')"
            :inactive-text="$t('禁用')" active-color="var(--huocms-com-color-primary)" inactive-color="rgba(0,0,0,0.2)"
            @change="disableUser(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="发布时间" prop="push_time" />
      <el-table-column :label="$t('操作')" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" class="edit" @click="editor(scope.row)">
            编辑
          </el-button>
          <el-button size="mini" class="dele" @click="del(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page.currentPage" :page-sizes="page.pageSizes" :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper" :total="page.total"
      :hide-on-single-page="page.total < page.size ? true : false" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
</template>
<script>
// import articleApi from '@/api/article.js'
// import contentApi from '@/api/content'
export default {
  data() {
    return {
      options: [], // 文章分类
      orderData: [], // 文章
      searchForm: {
        cate_id: '',
        title: ''
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {},
      totalAmount: ''
    }
  },
  created() {
    // this.getList()
    // this.getCateList()
  },
  mounted() {
    // this.getTikets()
  },
  updated() { },
  methods: {
    // 获取文章分类
    getCateList() {
      contentApi.getList().then(res => {
        this.options = res.data
      })
    },
    handleChange(value) {
      this.searchForm.cate_id = value[value.length - 1]
    },
    // 新增
    goAdd() {
      this.$router.push({ name: 'contentAdd' })
    },
    // 编辑
    editor(row) {
      this.$router.push({ name: 'contentAdd', query: { id: row.id } })
    },
    // 查看
    check(row) {
      articleApi.articleDetail({ id: row.id }).then(res => { })
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(() => {
          articleApi
            .delArticle({ id: row.id })
            .then(res => {
              if (res.code == 0) {
                this.getList()
                this.$message.success(res.msg)
              }
            })
            .catch(res => { })
        })
        .catch(rej => { })
    },
    // 文章列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      this.val.title = this.searchForm.title
      this.val.cate_id = this.searchForm.cate_id
      articleApi.getList(this.val).then(res => {
        this.page.total = res.count
        this.orderData = res.data
      })
    },
    // 禁用
    disableUser(row) {
      articleApi.changeStatus({ id: row.id, status: row.status }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        }
      })
    },
    // 搜索
    search() {
      this.$refs.searchForm.validate(valid => {
        this.page.currentPage = 1
        this.getList(this.val)
      })
    },
    // 重置搜索框
    reset(searchForm) {
      this.searchForm = {
        title: ''
      }
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }
  }
}
</script>
<style lang="scss">
#order {
  .el-date-editor.el-input.el-date-editor--date {
    width: 180px;
  }
}
</style>
<style scoped lang="scss">
.el-table {
  margin-bottom: 20px;
}

.search {
  min-width: 1350px;
  background-color: #fff;
  padding: 35px 30px 20px;
  border-radius: 5px;
}
</style>
